home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + _point.c
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
- #include <LEDA/plane.h>
- #include <math.h>
- #include <ctype.h>
-
- static const double eps = 1e-10;
-
-
- //------------------------------------------------------------------------------
- // points
- //------------------------------------------------------------------------------
-
- point_rep::point_rep() { count=1; x = y = 0.0; }
-
- point_rep::point_rep(double a, double b)
- { x = a;
- y = b;
- count = 1;
- }
-
-
-
- point::point() { ptr = new point_rep; }
- point::point(int) { ptr = new point_rep; }
- point::point(ent x) { ptr= (point_rep*)x; ptr->count++; }
- point::point(double x, double y){ ptr = new point_rep(x,y); }
- point::point(vector v) { ptr = new point_rep(v[0], v[1]); }
- point::point(point& p) { ptr = p.ptr; ptr->count++; }
- void point::clear() { if (--(ptr->count)==0) delete ptr; }
-
- point point::rotate(point origin, double alpha)
- { if (origin == *this) return *this;
- segment s(origin,*this);
- return s.rotate(alpha).end();
- }
-
- point point::rotate(double alpha)
- { return rotate(point(0,0),alpha);
- }
-
- point point::translate(double alpha, double d)
- { double dx = cos(alpha) * d;
- double dy = sin(alpha) * d;
- return point(ptr->x+dx,ptr->y+dy);
- }
-
- point point::translate(const vector& v)
- { return point(ptr->x+v[0],ptr->y+v[1]);
- }
-
- double point::distance(point p)
- { return hypot(p.ptr->x - ptr->x, p.ptr->y - ptr->y); }
-
- double point::distance()
- { return distance(point(0,0)); }
-
- point& point::operator=(const point& p)
- { p.ptr->count++;
- if (--ptr->count == 0) delete ptr;
- ptr = p.ptr;
- return *this;
- }
-
- int point::operator==(const point& p)
- { return (fabs(ptr->x - p.ptr->x) < eps && fabs(ptr->y - p.ptr->y) < eps); }
-
-
- ostream& operator<<(ostream& out, const point& p)
- { out << "(" << p.xcoord() << "," << p.ycoord() << ")";
- return out;
- }
-
- istream& operator>>(istream& in, point& p)
- { // syntax: {(} x {,} y {)}
-
- double x,y;
- char c;
-
- do in.get(c); while (isspace(c));
- if (c != '(') in.putback(c);
-
- in >> x;
-
- do in.get(c); while (isspace(c));
- if (c != ',') in.putback(c);
-
- in >> y;
-
- do in.get(c); while (c == ' ');
- if (c != ')') in.putback(c);
-
- p = point(x,y);
- return in;
-
- }
-
- int compare(point& a, point& b)
- {
- real xa = a.ptr->x;
- real xb = b.ptr->x;
- real ya = a.ptr->y;
- real yb = b.ptr->y;
- int r = compare(xa,xb);
- if (r==0) return compare(ya,yb);
- return r;
- }
-
-
-